//
//  MainTabBar.swift
//  sinaWeibo
//
//  Created by wan on 15/12/14.
//  Copyright © 2015年 wan. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {
    //UIView的默认的构造方法
    //重写父类的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeBtn)
    }

    required init?(coder aDecoder: NSCoder) {
        
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        addSubview(composeBtn)
    }
    //在这个方法中设置所有的子视图的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        //修改所有的tabbarbutton的frame + 将中间的位置空出来
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w , height: h )
        var index: CGFloat = 0
        //遍历所有的子视图  修改大小
        for subview in subviews{
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
            //修改frame
                subview.frame = CGRectOffset(rect, w * index, 0)
                //累加
                //三目
                index += (index == 1 ? 2 : 1)
            }
            //设置 加好按钮的位置
        }
        composeBtn.frame = CGRectOffset(rect, w * 2, 0)
        
//        bringSubviewToFront(composeBtn)

    }
    
    //懒加载 加号按钮
    lazy var composeBtn:UIButton = {
        //() 自定义样式
    let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState:UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        //根据视图 自动设置大小
        btn.sizeToFit()


        return btn
    }()
}
